<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="handleSearchList()" size="s米糕">
          查询搜索
        </el-button>
        <el-button style="float:right;margin-right: 15px" @click="handleResetSearch()" size="s米糕">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="s米糕" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input type="number" v-model="listQuery.orderSnId" class="input-width" placeholder="订单编号"></el-input>
          </el-form-item>
          <el-form-item label="购买者：">
            <el-input v-model="listQuery.userName" placeholder="收货人姓名/手机号码"></el-input>
          </el-form-item>
          <el-form-item label="提交时间：">
            <el-date-picker class="input-width" v-model="listQuery.createTime" value-format="yyyy-MM-dd" type="date"
              placeholder="请选择时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="订单状态：">
            <el-select v-model="listQuery.orderStatus" class="input-width" placeholder="全部" clearable>
              <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="订单来源：">
            <el-select v-model="listQuery.platform" class="input-width" placeholder="全部" clearable>
              <el-option v-for="item in sourceTypeOptions" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-table ref="orderTable" :data="list" style="width: 100%;" @selection-change="handleSelectionChange"
        v-loading="listLoading" @clearSelection="clearSelection" border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="80" align="center">
          <template slot-scope="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="订单编号" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.orderSnId }}</template>
        </el-table-column>
        <el-table-column label="提交时间" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.createTime | formatCreateTime }}</template>
        </el-table-column>
        <el-table-column label="用户账号" align="center">
          <template slot-scope="scope">{{ scope.row.userName }}</template>
        </el-table-column>
        <el-table-column label="订单金额" width="120" align="center">
          <template slot-scope="scope">￥{{ scope.row.priceSum }}</template>
        </el-table-column>
        <el-table-column label="支付方式" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.payType | formatPayType }}</template>
        </el-table-column>
        <el-table-column label="订单来源" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.platform | formatSourceType }}</template>
        </el-table-column>
        <el-table-column label="订单状态" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.orderStatus | formatStatus }}</template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleViewOrder(scope.$index, scope.row)">查看订单</el-button>
            <el-button size="mini" @click="handleCloseOrder(scope.$index, scope.row)"
              v-show="scope.row.orderStatus === 0">关闭订单
            </el-button>
            <!-- <el-button size="mini" @click="handleDeliveryOrder(scope.$index, scope.row)" v-show="scope.row.status === 1">
              订单发货</el-button>
            <el-button size="mini" @click="handleViewLogistics(scope.$index, scope.row)"
              v-show="scope.row.status === 2 || scope.row.status === 3">订单跟踪</el-button>
            <el-button size="mini" type="danger" @click="handleDeleteOrder(scope.$index, scope.row)"
              v-show="scope.row.status === 4">删除订单</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select size="s米糕" v-model="operateType" placeholder="批量操作">
        <el-option v-for="item in operateOptions" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <el-button style="margin-left: 20px" class="search-button" @click="handleBatchOperate()" type="primary"
        size="s米糕">
        确定
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        layout="total,prev, pager, next,jumper" :current-page.sync="listQuery.pageNum" :page-size="listQuery.pageSize"
        :page-sizes="[15]" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="关闭订单" :visible.sync="closeOrder.dialogVisible" width="30%">
      <span style="vertical-align: top">操作备注：</span>
      <el-input style="width: 80%" type="textarea" :rows="5" placeholder="请输入内容" v-model="closeOrder.content">
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeOrder.dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleCloseOrderConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { fetchList, closeOrder, deleteOrder, fetchListBySearch } from "../../../api/order"
import { OrderData } from "../../../data/ControllerData";
import OrderFilter from "../../../filter/order"

const defaultListQuery = {
  pageNum: 1,
  pageSize: 15,
  orderSnId: null,
  receiverKeyword: null,
  status: null,
  orderType: null,
  sourceType: null,
  createTime: null,
};
export default {
  name: "orderList",
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      listLoading: true,
      list: [],
      total: 0,
      operateType: null,
      multipleSelection: [],
      closeOrder: {
        dialogVisible: false,
        content: "",
        orderIds: [],
        orderIndexs: [],
      },
      logisticsDialogVisible: false,
      ...OrderData
    }
  },
  created() {
    this.getList();
  },
  filters: OrderFilter,
  methods: {

    clearSelection() {
      this.$refs.orderTable.clearSelection();
    },

    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      fetchListBySearch(this.listQuery, this);
    },
    handleSelectionChange(val, index) {

      console.log("val", val);
      val.forEach((e) => {
        this.list.forEach((item, i) => {
          if (e.id == item.id && 0 == item.orderStatus) {
            this.closeOrder.orderIndexs.push(i)
          }
        })
      })


      console.log("this.closeOrder.orderIndexs", this.closeOrder.orderIndexs)

      this.multipleSelection = val;
    },
    handleViewOrder(index, row) {
      this.$router.push({ path: '/oms/orderDetail', query: { id: row.id } })
    },
    handleCloseOrder(index, row) {
      this.closeOrder.dialogVisible = true;
      this.closeOrder.orderIds = [row.id];

      this.closeOrder.orderIndexs = [index];
    },
    handleDeliveryOrder(index, row) {
      let listItem = this.covertOrder(row);
      this.$router.push({ path: '/oms/deliverOrderList', query: { list: [listItem] } })
    },
    handleViewLogistics(index, row) {
      this.logisticsDialogVisible = true;
    },
    handleDeleteOrder(index, row) {
      let ids = [];
      ids.push(row.id);
      this.deleteOrder(ids);
    },
    handleBatchOperate() {
      if (this.multipleSelection == null || this.multipleSelection.length < 1) {
        this.$message({
          message: '请选择要操作的订单',
          type: 'warning',
          duration: 1000
        });
        return;
      }

      if (this.operateType === 2) {
        //关闭订单
        this.closeOrder.orderIds = [];

        for (let i = 0; i < this.multipleSelection.length; i++) {
          if (1 == this.multipleSelection[i].orderStatus ||
            -1 == this.multipleSelection[i].orderStatus
          ) {
            continue;
          }


          this.closeOrder.orderIds.push(this.multipleSelection[i].id);
        }

        if (0 === this.closeOrder.orderIds.length) {
          this.$message({
            message: '请选择要操作的行或选择的订单已关闭',
            type: 'warning',
            duration: 1000
          });
          return;
        }

        this.closeOrder.dialogVisible = true;
      } else if (this.operateType === 3) {
        //删除订单
        let ids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].id);
        }
        this.deleteOrder(ids);
      }
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleCloseOrderConfirm() {

      console.log("this.closeOrder.orderIndexs", this.closeOrder.orderIndexs);

      if (0 == this.closeOrder.orderIds.length) {
        this.$message({
          message: '请选择要操作的行或选择的订单已关闭',
          type: 'warning',
          duration: 1000
        });
        this.closeOrder.dialogVisible = false;
        return;
      }
      if (this.closeOrder.content == null || this.closeOrder.content === '') {
        this.$message({
          message: '操作备注不能为空',
          type: 'warning',
          duration: 1000
        });
        return;
      }



      let params = {
        'ids': this.closeOrder.orderIds,
        'remarks': this.closeOrder.content
      };

      closeOrder(params).then(response => {

        this.closeOrder.dialogVisible = false;
        this.$message({
          message: '修改成功',
          type: 'success',
          duration: 1000
        });
        this.closeOrder.orderIndexs.forEach(e => {
          this.list[e].orderStatus = -1;
        })
        this.closeOrder.orderIds = [];
        this.closeOrder.orderIndexs = [];

        this.multipleSelection = [];
        this.clearSelection();
        console.log("this.list", this.list)
      });
    },
    getList() {
      console.log('this', this);
      this.listLoading = true;
      fetchList(this.listQuery, this);
    },
    deleteOrder(ids) {
      this.$confirm('是否要进行该删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = new URLSearchParams();
        params.append("ids", ids);
        deleteOrder(params).then(response => {
          this.$message({
            message: '删除成功！',
            type: 'success',
            duration: 1000
          });
          this.getList();
        });
      })
    },
    covertOrder(order) {
      let address = order.receiverProvince + order.receiverCity + order.receiverRegion + order.receiverDetailAddress;
      let listItem = {
        orderId: order.id,
        orderSn: order.orderSn,
        receiverName: order.receiverName,
        receiverPhone: order.receiverPhone,
        receiverPostCode: order.receiverPostCode,
        address: address,
        deliveryCompany: null,
        deliverySn: null
      };
      return listItem;
    }
  }
}
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>


